package com.wing.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.wing.common.utils.HttpHelper;
import com.wing.common.utils.JsonResult;
import com.wing.order.service.DynamicService;
import com.wing.product.model.entity.*;
import com.wing.product.service.*;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 基础数据控制器
 *
 * @author Administrator
 * @date 2022/06/22
 */
@Validated
@RestController("cabCommonController")
@RequestMapping("common/admin/cabCommon")
@RequiredArgsConstructor
public class CabCommonController {

    @Value("${platform.gatewayUrl}")
    private String url;

    @Autowired
    private ProductUnitService productUnitService;

    @Autowired
    private ProductAttributeService productAttributeService;

    @Autowired
    private ProductAttributeValueService productAttributeValueService;

    @Autowired
    private ProductBrandService productBrandService;

    @Autowired
    private ProductCategoryService productCategoryService;

    @Autowired
    private ProductCategoryExtService productCategoryExtService;

    @Autowired
    private ProductMaterialCategoryService productMaterialCategoryService;

    @Autowired
    private ProductSpecService productSpecService;

    @Autowired
    private ProductSpecValueService productSpecValueService;

    @Autowired
    private ProductTypeService productTypeService;

    @Autowired
    private DynamicService dynamicService;

    /**
     * 同步产品单位
     * @return {@link JsonResult}
     */
    @ApiOperation(value = "同步产品单位", notes = "同步")
    @PostMapping("/synchronizeProductUnit")
    public JsonResult synchronizeProductUnit() throws Exception {
        final String reqUri = url+"platform/product/admin/productUnit/list?page=1&limit=1000";
        String tokenJson = HttpHelper.httpGet(reqUri);
        //String数据转JSONObject对象
        JSONObject jsonObject = JSON.parseObject(tokenJson);
        //获取data对应数据转JSONArray
        JSONArray array = jsonObject.getJSONArray("data");
        //JSONArray 转list对象数组
        List<ProductUnit> list = JSONArray.parseArray(array.toJSONString(),ProductUnit.class);
        if(list.size() > 0 ){
            productUnitService.saveOrUpdateBatch(list);
        }
        return JsonResult.success(tokenJson);
    }

    /**
     * 同步产品类型属性
     * @return {@link JsonResult}
     */
    @ApiOperation(value = "同步产品类型属性", notes = "同步")
    @PostMapping("/synchronizeProductAttribute")
    public JsonResult synchronizeProductAttribute() throws Exception {
        final String reqUri = url+"platform/product/admin/productAttribute/list?page=1&limit=1000";
        String tokenJson = HttpHelper.httpGet(reqUri);
        //String数据转JSONObject对象
        JSONObject jsonObject = JSON.parseObject(tokenJson);
        //获取data对应数据转JSONArray
        JSONArray array = jsonObject.getJSONArray("data");
        //JSONArray 转list对象数组
        List<ProductAttribute> list = JSONArray.parseArray(array.toJSONString(),ProductAttribute.class);
        if(list.size() > 0 ){
            productAttributeService.saveOrUpdateBatch(list);
        }
        return JsonResult.success(tokenJson);
    }

    /**
     * 同步产品类型属性值
     * @return {@link JsonResult}
     */
    @ApiOperation(value = "同步产品类型属性值", notes = "同步")
    @PostMapping("/synchronizeProductAttributeValue")
    public JsonResult synchronizeProductAttributeValue() throws Exception {
        final String reqUri = url+"platform/product/admin/productAttributeValue/list?page=1&limit=1000";
        String tokenJson = HttpHelper.httpGet(reqUri);
        //String数据转JSONObject对象
        JSONObject jsonObject = JSON.parseObject(tokenJson);
        //获取data对应数据转JSONArray
        JSONArray array = jsonObject.getJSONArray("data");
        //JSONArray 转list对象数组
        List<ProductAttributeValue> list = JSONArray.parseArray(array.toJSONString(),ProductAttributeValue.class);
        if(list.size() > 0 ){
            productAttributeValueService.saveOrUpdateBatch(list);
        }
        return JsonResult.success(tokenJson);
    }

    /**
     * 同步产品品牌
     * @return {@link JsonResult}
     */
    @ApiOperation(value = "同步产品品牌", notes = "同步")
    @PostMapping("/synchronizeProductBrand")
    public JsonResult synchronizeProductBrand() throws Exception {
        final String reqUri = url+"platform/product/admin/productBrand/list?page=1&limit=1000";
        String tokenJson = HttpHelper.httpGet(reqUri);
        //String数据转JSONObject对象
        JSONObject jsonObject = JSON.parseObject(tokenJson);
        //获取data对应数据转JSONArray
        JSONArray array = jsonObject.getJSONArray("data");
        //JSONArray 转list对象数组
        List<ProductBrand> list = JSONArray.parseArray(array.toJSONString(),ProductBrand.class);
        if(list.size() > 0 ){
            productBrandService.saveOrUpdateBatch(list);
        }
        return JsonResult.success(tokenJson);
    }

    /**
     * 同步产品分类
     * @return {@link JsonResult}
     */
    @ApiOperation(value = "同步产品分类", notes = "同步")
    @PostMapping("/synchronizeProductCategory")
    public JsonResult synchronizeProductCategory() throws Exception {
        final String reqUri = url+"platform/product/admin/productCategory/list?page=1&limit=1000";
        String tokenJson = HttpHelper.httpGet(reqUri);
        //String数据转JSONObject对象
        JSONObject jsonObject = JSON.parseObject(tokenJson);
        //获取data对应数据转JSONArray
        JSONArray array = jsonObject.getJSONArray("data");
        //JSONArray 转list对象数组
        List<ProductCategory> list = JSONArray.parseArray(array.toJSONString(),ProductCategory.class);
        if(list.size() > 0 ){
            productCategoryService.saveOrUpdateBatch(list);
        }
        return JsonResult.success(tokenJson);
    }

    /**
     * 同步产品分类扩展子表
     * @return {@link JsonResult}
     */
    @ApiOperation(value = "同步产品分类扩展子表", notes = "同步")
    @PostMapping("/synchronizeProductCategoryExt")
    public JsonResult synchronizeProductCategoryExt() throws Exception {
        final String reqUri = url+"platform/product/admin/productCategoryExt/list?page=1&limit=1000";
        String tokenJson = HttpHelper.httpGet(reqUri);
        //String数据转JSONObject对象
        JSONObject jsonObject = JSON.parseObject(tokenJson);
        //获取data对应数据转JSONArray
        JSONArray array = jsonObject.getJSONArray("data");
        //JSONArray 转list对象数组
        List<ProductCategoryExt> list = JSONArray.parseArray(array.toJSONString(),ProductCategoryExt.class);
        if(list.size() > 0 ){
            productCategoryExtService.saveOrUpdateBatch(list);
        }
        return JsonResult.success(tokenJson);
    }

    /**
     * 同步材料分类
     * @return {@link JsonResult}
     */
    @ApiOperation(value = "同步材料分类", notes = "同步")
    @PostMapping("/synchronizeProductMaterialCategory")
    public JsonResult synchronizeProductMaterialCategory() throws Exception {
        final String reqUri = url+"platform/product/admin/productMaterialCategory/list?page=1&limit=1000";
        String tokenJson = HttpHelper.httpGet(reqUri);
        //String数据转JSONObject对象
        JSONObject jsonObject = JSON.parseObject(tokenJson);
        //获取data对应数据转JSONArray
        JSONArray array = jsonObject.getJSONArray("data");
        //JSONArray 转list对象数组
        List<ProductMaterialCategory> list = JSONArray.parseArray(array.toJSONString(),ProductMaterialCategory.class);
        if(list.size() > 0 ){
            productMaterialCategoryService.saveOrUpdateBatch(list);
        }
        return JsonResult.success(tokenJson);
    }

    /**
     * 同步产品类型规格
     * @return {@link JsonResult}
     */
    @ApiOperation(value = "同步产品类型规格", notes = "同步")
    @PostMapping("/synchronizeProductSpec")
    public JsonResult synchronizeProductSpec() throws Exception {
        final String reqUri = url+"platform/product/admin/productSpec/list?page=1&limit=1000";
        String tokenJson = HttpHelper.httpGet(reqUri);
        //String数据转JSONObject对象
        JSONObject jsonObject = JSON.parseObject(tokenJson);
        //获取data对应数据转JSONArray
        JSONArray array = jsonObject.getJSONArray("data");
        //JSONArray 转list对象数组
        List<ProductSpec> list = JSONArray.parseArray(array.toJSONString(),ProductSpec.class);
        if(list.size() > 0 ){
            productSpecService.saveOrUpdateBatch(list);
        }
        return JsonResult.success(tokenJson);
    }

    /**
     * 同步产品类型规格值
     * @return {@link JsonResult}
     */
    @ApiOperation(value = "同步产品类型规格值", notes = "同步")
    @PostMapping("/synchronizeProductSpecValue")
    public JsonResult synchronizeProductSpecValue() throws Exception {
        final String reqUri = url+"platform/product/admin/productSpecValue/list?page=1&limit=1000";
        String tokenJson = HttpHelper.httpGet(reqUri);
        //String数据转JSONObject对象
        JSONObject jsonObject = JSON.parseObject(tokenJson);
        //获取data对应数据转JSONArray
        JSONArray array = jsonObject.getJSONArray("data");
        //JSONArray 转list对象数组
        List<ProductSpecValue> list = JSONArray.parseArray(array.toJSONString(),ProductSpecValue.class);
        if(list.size() > 0 ){
            productSpecValueService.saveOrUpdateBatch(list);
        }
        return JsonResult.success(tokenJson);
    }


    /**
     * 同步产品类型
     * @return {@link JsonResult}
     */
    @ApiOperation(value = "同步产品类型", notes = "同步")
    @PostMapping("/synchronizeProductType")
    public JsonResult synchronizeProductType() throws Exception {
        final String reqUri = url+"platform/product/admin/productType/list?page=1&limit=1000";
        String tokenJson = HttpHelper.httpGet(reqUri);
        //String数据转JSONObject对象
        JSONObject jsonObject = JSON.parseObject(tokenJson);
        //获取data对应数据转JSONArray
        JSONArray array = jsonObject.getJSONArray("data");
        //JSONArray 转list对象数组
        List<ProductType> list = JSONArray.parseArray(array.toJSONString(),ProductType.class);
        if(list.size() > 0 ){
            productTypeService.saveOrUpdateBatch(list);
        }
        return JsonResult.success(tokenJson);
    }
    @ApiOperation(value = "查询企业名字", notes = "查询")
    @GetMapping("/getEnterpriseName")
    public JsonResult getEnterpriseName(){
        String enterpriseName = dynamicService.getEnterpriseName();
        return JsonResult.success(enterpriseName);
    }
}
